
package ch.hearc.coursJava.exa.swing;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;

import ch.hearc.coursJava.GUI.j2D.tools.JPanelDessin_A;

public class TableDessin extends JPanelDessin_A implements Runnable
    {
    
    /**
     * 
     */
    private static final long serialVersionUID = -2474017768393942233L;
    
    /*------------------------------------------------------------------*\
    |*							Constructeurs							*|
    \*------------------------------------------------------------------*/
    public TableDessin(JFrame frame)
        {
        this.starWritted = false;
        this.frame = frame;
        this.listPoint = new LinkedList<Point>();
        this.color = Color.BLUE;
        this.setBackground(Color.WHITE);
        
        addListener();
        }
    
    /*------------------------------------------------------------------*\
    |*							Methodes Public							*|
    \*------------------------------------------------------------------*/
    @Override
    protected void dessiner(Graphics2D g2d)
        {
        g2d.setColor(this.color);
        if (listPoint.size() >= 2)
            {
            for(int i = 1; i < listPoint.size(); i += 2)
                {
                g2d.drawLine((int)listPoint.get(i - 1).getX(), (int)listPoint.get(i - 1).getY(), (int)listPoint.get(i).getX(), (int)listPoint.get(i).getY());
                }
            }
        }
    
    @Override
    public void run()
        {
        
        }
    
    /*------------------------------*\
    |*				Get				*|
    \*------------------------------*/
    public Color getColor()
        {
        return this.color;
        }
    
    public List<Point> getListPoint()
        {
        return this.listPoint;
        }
    
    /*------------------------------*\
    |*				Set				*|
    \*------------------------------*/
    public void setColor(Color color)
        {
        this.color = color;
        }
    
    public void setListPoint(List<Point> listPoint)
        {
        this.listPoint = listPoint;
        }
    
    public void setStarWritted(boolean starWritted)
        {
        this.starWritted = starWritted;
        }
    
    /*------------------------------------------------------------------*\
    |*							Methodes Private						*|
    \*------------------------------------------------------------------*/
    private void addListener()
        {
        this.addMouseListener(new MouseAdapter()
            {
                
                @Override
                public void mousePressed(MouseEvent arg0)
                    {
                    if (arg0.getButton() == MouseEvent.BUTTON1)
                        {
                        pointA = arg0.getPoint();
                        }
                    }
            });
        
        this.addMouseMotionListener(new MouseMotionAdapter()
            {
                
                @Override
                public void mouseDragged(MouseEvent e)
                    {
                    
                    MainWindow m = (MainWindow)frame;
                    m.setIsSave(false);
                    if (!starWritted && m.getNomFichier() != null)
                        {
                        m.setTitle(m.getTitle() + " *");
                        starWritted = true;
                        }
                    
                    listPoint.add(pointA);
                    pointB = e.getPoint();
                    listPoint.add(pointB);
                    pointA = pointB;
                    repaint();
                    
                    }
            });
        }
    
    /*------------------------------------------------------------------*\
    |*							Attributs Private						*|
    \*------------------------------------------------------------------*/
    //input
    private JFrame frame;
    private boolean starWritted;
    
    //tools
    private Point pointA;
    private Point pointB;
    private List<Point> listPoint;
    private Color color;
    
    }
